package org.ovgu.coursify;

import java.text.SimpleDateFormat;
import java.util.List;

import org.ovgu.coursify.data.Comment;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CommentAdapter extends ArrayAdapter<Comment> {

	private final int resource;
	private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
			"dd.MM.yyyy HH:mm");

	public CommentAdapter(Context context, int resource, List<Comment> objects) {
		super(context, resource, objects);
		this.resource = resource;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Comment comment = getItem(position);
		LayoutInflater inflater = (LayoutInflater) getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		RelativeLayout view = (RelativeLayout) inflater.inflate(resource,
				parent, false);
		TextView commentTextView = (TextView) view
				.findViewById(R.id.commentTextView);
		commentTextView.setText(comment.getContent());
		TextView commentRating = (TextView) view
				.findViewById(R.id.commetnRatingView);
		commentRating.setText("Rating: " + comment.getRating());
		TextView commentDate = (TextView) view
				.findViewById(R.id.commentDateTimeView);
		commentDate.setText( simpleDateFormat.format(comment.getDateTime()));
		return view;
	}
}
